# $Id: Muckery.pm 36 2006-09-29 23:09:30Z cmauch $
# Author: Charles Mauch (cmauch@gmail.com)
# Description: Module to handle email name mangling for filing
# Legal: (c) 2006 Charles Mauch, released under the terms of the GPLv2 or later

# -- [ sub: hashname ] ------------------------------------------------------
#
#Summary:      Subroutine to hash a name like 'hawkins' into something
#              like h/ha/hawkins
#Parameters:   Full Name, eg: "hawkins", allready lowercased
#Return:       a reformatted string, in the format h/ha/hawkins
#
sub hashname {
    my $surname = $_[0];
    $surname =~ /^((.).).*/;
    if ( defined $1 && defined $2 ) {

        # return ("$2/$1/$surname"); # don't need to dig that deep, h/hawkins
        # this would return h/ha/hawkins
        return ( "$2/$surname" );
    } else {
        return ( $surname );
    }
} ## end sub hashname

# -- [ sub: getfoldername ] -------------------------------------------------
#
#Summary:      Subroutine to transform a full name like "David Hawkins" into
#              a reversed foldername.
#Parameters:   Full Name, eg: "David Hawkins"
#Return:       a reformatted string, in the format h/ha/hawkins/dave

sub getfoldername {
    my $fullname = lc( $_[0] );
    my ( $foldername, $first, $last );

    # is it just firstname lastname?
    if ( $fullname =~ /^([a-z]+)\s+([a-z]+)$/ ) {
        $first = $1;
        $last  = $2;

        # round the other way?, damned microsoft
    } elsif ( $fullname =~ /^([a-z]+)\s*,\s*([a-z]+)/ ) {
        $first = $2;
        $last  = $1;
    } elsif ( $fullname =~ /^([a-z]+)\s+.*\s+([a-z]+)$/ ) {

        # more than one name?
        $first = $1;
        $last  = $2;
    } elsif ( $fullname =~ /^([a-z]+)\s+([a-z]+)\s+([a-z]+)/ ) {

        # Three part name
        $first = "$1$2";
        $last  = $3;
    } elsif ( $fullname =~ /^([a-z]+)\s+([a-z]+)-+([a-z]+)/ ) {

        # Dashed Last Names (like Ian Hall-Beyer)
        $first = $1;
        $last  = "$2$3";
    } elsif ( $fullname =~ /^([a-z]+)\s+([a-z]+)'+([a-z]+)/ ) {

        # Names with a SingleQuote (like James O'Neill)
        $first = $1;
        $last  = "$2$3";
    } elsif ( $fullname =~ /^([a-z]+)$/ ) {

        # We just give up, Deal with One Name only (The Last)
        $first = "00unknown";
        $last  = $1;
    }

    # now construct folder name, based on first, last (or didn't)
    if ( defined $first && defined $last ) {
        $foldername = hashname( $last ) . "." . $first;
    } else {

        # no firstname and lastname
        $foldername = "00unknown";
    }

    # strip spaces, replace with dots and spaces with slashes
    $foldername =~ s/\./\//g;
    $foldername =~ s/ /\//g;
    return ( $foldername );
} ## end sub getfoldername

# -- [ sub: getlistname ] ---------------------------------------------------
#
#Summary:      Subroutine to transform an email addresse into a foldername
#Parameters:   Email Address, eg: "vim-help@vim.org"
#Return:       a reformatted string, in the format org/vim/vim-help

sub getlistname {
    my $from = $_[0];
    my $email;

    # if there is a '+' sign in the email, we'll strip it first.
    $from =~ s/.*\+//;

    if ( $from =~ /([A-Za-z\-_0-9\@\.]+)/ ) {

        # split the email into username and domain
        my ( $username, $domain ) = split( /@/, $from );

        # change any '.' before the @ into _, so they don't become
        # hierarchy delimiters
        $username =~ s/\./_/g;

        # we reverse the order of the domain, to get a nice hierarchy
        # of email addresses by domain level
        $domain = join( '.', reverse( split /\./, $domain ) );

        # re-assemble
        $email = $domain . "." . $username;
    } else {
        $email = "00unknown";
    }

    # smash case and remove bad stuff
    $email =~ tr/A-Z/a-z/;
    $email =~ tr/a-z0-9_\-@\.//cd;

    # Now, sort it all into hiearchial folders, the way it should be.
    $email =~ s/\./\//g;

    #$folder =~ s/\./\//g;

    # return the folder name
    return ( $email );
} ## end sub getlistname

1;
